//
//  XKMineAddBankCardController.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/14.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKAddBankCardController.h"
#import "XKAddBankCardView.h"
#import "BankPickView.h"
@interface XKAddBankCardController ()

@property (nonatomic, strong) XKAddBankCardView *addbankCardView;

@property (nonatomic, strong) BankPickView *bankSelectPickerView;

@end

@implementation XKAddBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupSubviews];
    
//    @weakify(self);
//    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:@"nnnn" timeInterval:60 queue:dispatch_get_main_queue() repeats:YES actionOption:AbandonPreviousAction action:^{
//        @strongify(self);
//        
//        NSArray *bankNameArray = @[@"北京银行",@"中国银行",@"中国建设银行",@"中国农业银行",@"浦发银行",@"中国邮政银行",@"广发银行",@"中国工商银行"];
//        [self addBankCardWithUserName:@"测试" cardNumber:[NSString stringWithFormat:@"%zd",arc4random() % 1000000000000001 + 1000000000000000] cardName:bankNameArray[arc4random()%8]];
//    }];
    
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [SVProgressHUD dismiss];
}

#pragma mark - 设置UI
- (void)setupUI {
    self.navigationItem.title = @"添加银行卡";
}

#pragma mark - 设置子视图
- (void)setupSubviews {
    
    self.addbankCardView = [XKAddBankCardView new];
    self.addbankCardView.frame = self.view.bounds;
    [self.view addSubview:self.addbankCardView];
    @weakify(self);
    self.addbankCardView.AddBankCardBlock = ^(NSString *userName, NSString *cardNumber, NSString *cardName){
        @strongify(self);
        [self addBankCardWithUserName:userName cardNumber:cardNumber cardName:cardName];
    };
    
    self.addbankCardView.BankNameSelectBlock = ^(){
        @strongify(self);
        [self.bankSelectPickerView show];
    };
}

- (BankPickView *)bankSelectPickerView {
    if (!_bankSelectPickerView) {
        _bankSelectPickerView = [BankPickView new];
        @weakify(self);
        _bankSelectPickerView.confimBtnActionBlcok = ^(NSString *text) {
            @strongify(self);
            if (![text isEqualToString:@"请选择银行"]) {
                self.addbankCardView.bankNameText = text;
            }
        };
    }
    return _bankSelectPickerView;
}

#pragma mark - 添加银行卡
- (void)addBankCardWithUserName:(NSString *)userName cardNumber:(NSString *)cardNumber cardName:(NSString *)cardName{
    
    if (userName == nil || [userName isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请输入开户人姓名!"];
        return;
    }
    
    if (![userName isValidRealName]) {
        [SVProgressHUD showErrorWithStatus:@"请输入真实姓名!"];
        return;
    }
    
    if (IsEmptyStr(cardName)) {
        [SVProgressHUD showErrorWithStatus:@"请选择银行名称!"];
        return;
    }
    
    if (IsEmptyStr(cardNumber)) {
        [SVProgressHUD showErrorWithStatus:@"请输入银行卡号!"];
        return;
    }
    
    if (!(cardNumber.length >= 16 && cardNumber.length <= 19)) {
        [SVProgressHUD showErrorWithStatus:@"银行卡号格式错误!"];
        return;
    }
    
    
    
    
    [self.view endEditing:YES];
    [SVProgressHUD show];
    
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_AddBankCard parameters:@{@"name":userName, @"bank":cardName, @"account":cardNumber} successBlock:^(NSDictionary *successDict) {
        [SVProgressHUD showSuccessWithStatus:@"银行卡添加成功!"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        });
        
        [MobClick event:@"AddBankCardSuccess"]; // 友盟事件统计
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
    
}



#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
